﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using jiaju.Models.DBModel;
using System.Data.SqlClient;
using jiaju.Models;

namespace jiaju.Models.DAL
{
    public class NewsService
    {
        public static List<News> getNews()
        {
            string sql = "select Id,Title,Cont,Time from dbo.news where HomeFlag='是'";
            List<News> list = new List<DBModel.News>();
            SqlDataReader sdr = DBHelp.ExecuteReader(sql);
            while (sdr.Read())
            {
                int Id = (int)sdr["Id"];
                string Title = (string)sdr["Title"];
                string Cont = (string)sdr["Cont"];
                DateTime Time = (DateTime)sdr["Time"];
                string NianYue = Time.Year + "/" + Time.Month;
                int Day = Time.Day;
                News news = new News()
                {
                    Id = Id,
                    Title = Title,
                    Cont = Cont,
                    NianYue = NianYue,
                    Day = Day
                };
                list.Add(news);
            }
            return list;
        }
        //内容详情
        public static News getNewsDetail(string Id)
        {
            string sql = "select Title,Cont from dbo.news where Id=" + Id;
            SqlDataReader sdr = DBHelp.ExecuteReader(sql);
            News NewsDel = null;
            while (sdr.Read())
            {
                string Title = (string)sdr["Title"];
                string Cont = (string)sdr["Cont"];
                NewsDel = new News()
                {
                    Title = Title,
                    Cont = Cont
                };
            }
            return NewsDel;
        }
        //获取新闻列表分页
        public static List<News> getNewsPage(int page, int limit)
        {
            string sql = string.Format(@"select top({1}) Id,Title,Cont,Time
                               from dbo.news where Id not in (
                              select top(({0}-1)*{1}) Id from dbo.news
                              )", page, limit);
            List<News> list = new List<DBModel.News>();
            SqlDataReader sdr = DBHelp.ExecuteReader(sql);
            while (sdr.Read())
            {
                int Id = (int)sdr["Id"];
                string Title = (string)sdr["Title"];
                string Cont = (string)sdr["Cont"];
                DateTime Time = (DateTime)sdr["Time"];
                string NianYue = Time.Year + "/" + Time.Month;
                int Day = Time.Day;
                News newsList = new News()
                {
                    Id = Id,
                    Title = Title,
                    Cont = Cont,
                    NianYue = NianYue,
                    Day = Day
                };
                list.Add(newsList);
            }
            return list;
        }
        //获取分类新闻分页
        public static List<News> getNewsPage2(int page, int limit, int id)
        {
            string sql = string.Format(@"select top({1}) Id,Title,Cont,Time
                               from dbo.news where Id not in (
                              select top(({0}-1)*{1}) Id from dbo.news
                              ) and TypeId={2}", page, limit, id);
            List<News> list = new List<DBModel.News>();
            SqlDataReader sdr = DBHelp.ExecuteReader(sql);
            while (sdr.Read())
            {
                int Id = (int)sdr["Id"];
                string Title = (string)sdr["Title"];
                string Cont = (string)sdr["Cont"];
                DateTime Time = (DateTime)sdr["Time"];
                string NianYue = Time.Year + "/" + Time.Month;
                int Day = Time.Day;
                News newsList = new News()
                {
                    Id = Id,
                    Title = Title,
                    Cont = Cont,
                    NianYue = NianYue,
                    Day = Day
                };
                list.Add(newsList);
            }
            return list;
        }
    }
}